<?php

namespace lico\Runtime;

class Config
{
    protected $_route;
    protected $_module;

    protected $config;
    protected $appConfig;
    protected $database;

    public function __construct(Route $route)
    {
        $this->_route = $route;
        $this->_module = $this->_route->module();
    }

    public function run()
    {

        $this->init();
        // $this->databaseInit();
        // $this->configInit();

        return true;
    }

    /**
     * 初始化
     */
    public function init()
    {
        if (is_null($this->config)) {
            $this->databaseInit();
            $this->configInit(); //p($this->config);
        }
    }

    /**
     * 数据库配置
     */
    public function databaseInit()
    {

        $userDatabase = constant("SYSCONFIG")['Database']['main'];

        $config = [];
        $databaseFile = ROOT_PATH . $userDatabase . DS . $this->_module . EXT;
        if (file_exists($databaseFile)) {
            $config = include $databaseFile;
        }

        $defaultConfig = include LICO_PATH . 'Const' . DS . 'database' . EXT;
        $newConfig = array_merge($defaultConfig, $config);
        // p($config,$defaultConfig,$newConfig);
        $this->database = $newConfig;
        $this->config['database'] = $this->database;
    }

    /**
     * 应用配置
     */
    public function configInit()
    {

        $userConfig = constant("SYSCONFIG")['Config']['main'];

        $config = [];
        $configFile = ROOT_PATH . $userConfig . DS . $this->_module . EXT;
        if (file_exists($configFile)) {
            $config = include $configFile;
        }

        $defaultConfig = include LICO_PATH . 'Const' . DS . 'config' . EXT;

        $newConfig = [];
        foreach ($config as $key => $value) {
            $newConfig[$key] = isset($defaultConfig[$key]) ? array_merge($defaultConfig[$key], $config[$key]) : $config[$key];
        }
        foreach ($defaultConfig as $key => $value) {
            !isset($config[$key]) && $newConfig[$key] = $defaultConfig[$key];
        }
        // p($config,$defaultConfig,$newConfig);
        $this->appConfig = $newConfig;
        $this->config['appConfig'] = $this->appConfig;
        $this->config['defaultConfig'] = $defaultConfig;
    }

    /**
     * 获取数据库配置
     */
    public function database($configName = "")
    {
        $this->init();
        if ($configName) {
            return isset($this->database[$configName]) ? $this->database[$configName] : [];
        } else {
            return $this->database;
        }
    }

    /**
     * 获取应用配置
     */
    public function app($configName = "")
    {
        $this->init();
        if ($configName) {
            return isset($this->appConfig[$configName]) ? $this->appConfig[$configName] : [];
        } else {
            return $this->appConfig;
        }
    }

    /**
     * 获取配置
     */
    public function get($configName = '')
    {
        $this->init();
        if ($configName) {
            return isset($this->config[$configName]) ? $this->config[$configName] : [];
        } else {
            return $this->config;
        }
    }

    /**
     * 默认配置
     */
    public function def($configName = '')
    {
        $defaultConfig = include LICO_PATH . 'Const' . DS . 'config' . EXT;
        if ($configName) {
            return isset($defaultConfig[$configName]) ? $defaultConfig[$configName] : [];
        } else {
            return $defaultConfig;
        }
    }
}
